/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package server;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class Mysql
{
	static Logger _log = Logger.getLogger(Mysql.class.getName());

	private static Mysql _instance;
	private ComboPooledDataSource _source;
	private static String database = "server";

	public Mysql() throws SQLException
	{
		try
		{
			_source = new ComboPooledDataSource(); // Nova instance modulu c3p0 - balicek pro komunikaci s mysql
			_source.setAutoCommitOnClose(true); // zmeny se samy ulozi pri uzavreni spojeni

			_source.setInitialPoolSize(10); 
			_source.setMinPoolSize(10);
			_source.setMaxPoolSize(10);

			_source.setAcquireRetryAttempts(0); // pocet pokusu o obnovu spojeni (0 = nekonecno)
			_source.setAcquireRetryDelay(500); // zpozdeni pred novym pokusem o spojeni v ms
			_source.setCheckoutTimeout(0); // cas po ktery se bude cekat na nove spojeni (0 = nekonecne)
			_source.setAcquireIncrement(5); // if pool is exhausted, get 5 more connections at a
											
			_source.setAutomaticTestTable("connection_test_table"); // testovaci tabulka pro vyzkouseni spojeni
			_source.setTestConnectionOnCheckin(false); // otestovat spoje pri kazdem pripojeni?
			
			_source.setIdleConnectionTestPeriod(3600); // kazdych 60s zkusit spojeni
			_source.setMaxIdleTime(0); // nikdy neodpojovat spojeni pokud je neaktivni
			_source.setMaxStatementsPerConnection(100); // maximalni pocet sql prikazu ve fornte

			_source.setBreakAfterAcquireFailure(false); // pokud nastane chyba a je nastaveno true mysql se zastavi 
														// a vyzaduje restart
			
			_source.setDriverClass("com.mysql.jdbc.Driver"); // ovladac mysql
			_source.setJdbcUrl("jdbc:mysql://localhost/" + database); // umisteni databaze
			_source.setUser("root"); // uzivatelske jmeno
			_source.setPassword("root"); // heslo
			
			_source.getConnection().close(); // tests spojeni
			_log.fine("Database Connection Working");
		}
		catch (Exception e)
		{
			_log.fine("Database Connection FAILED");
			throw new SQLException("Could not init DB connection:" + e.getMessage());
		}
	}

	public static Mysql getInstance() throws SQLException
	{
		synchronized (Mysql.class)
		{
			if (_instance == null)
			{
				_instance = new Mysql();
			}
		}
		return _instance;
	}

	public Connection getConnection()
	{
		Connection con = null;

		while (con == null)
		{
			try
			{
				con = _source.getConnection();
			}
			catch (SQLException e)
			{
				_log.log(Level.WARNING, "Database: getConnection() failed, trying again " + e.getMessage(), e);
			}
		}
		return con;
	}

}
